package com.vgooo.client.vchannel.album;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.vgooo.client.R;
import com.vgooo.client.ViewLayout;
import com.vgooo.client.vchannel.PingDetail;
import com.vgooo.util.BitmapUtils;
import com.vgooo.util.DensityUtil;

public class ViewAdapter extends PagerAdapter {

	private ArrayList<View> views;
	private ViewPager pager;
	private TextView pingButton ;//, allPager, currentPager;
	private String pings;
	private int currentView = 1;
	private static int pingViewIndex;
	private PingDetail pingDetail;
	private boolean hasPing;

	public ViewAdapter(final ViewPager pager, List<View> views,
			TextView pingButton, String pings, final PingDetail pingDetail
			) {
		this.pager = pager;
		this.pingButton = pingButton;
		this.pings = "评论(" + pings + ")";
		this.views = new ArrayList<View>() ;
		this.views.add(new ImageView(pager.getContext()));
		this.views.addAll(views);
		if (null != pingDetail) {
			hasPing = true;
			this.pingDetail = pingDetail;
			this.views.add(pingDetail.getmPullDownView());
			pingButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (currentView == pingViewIndex) {
						currentView--;
						setCurrentView();
					} else if (currentView != 0) {
						currentView = pingViewIndex;
						setCurrentView();
					}
				}
			});
		} else {
			hasPing = false;
		}
		
		this.currentView = 1;

		pingViewIndex = this.views.size() - 1;

		this.pager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				if (pingDetail != null && pingDetail.isEdit) {
					pingDetail.chanageEdit(false);
					// setCurrentView();
				} else {
					currentView = arg0;
				}
				setCurrentView();
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	private void setCurrentView() {
		if (currentView == 0) {
			currentView = 1;
			ViewLayout.setViewBack();
		} else if (hasPing) {
			if (currentView == pingViewIndex) {
				pingButton.setText("原文");
			} else {
				pingButton.setText(pings);
			}
		}
		pager.setCurrentItem(currentView);
	}

//	public void setPagerCount(TextView current, TextView all) {
//		this.allPager = all;
//		this.currentPager = current;
//		all.setText(views.size() - 1 + "");
//		currentPager.setText("1");
//		setCurrentView();
//	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return views.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		// TODO Auto-generated method stub
		View view = views.get(position);
		((ViewPager) container).removeView(view);
	}

	@Override
	public void finishUpdate(ViewGroup container) {
		// TODO Auto-generated method stub
		super.finishUpdate(container);
	}

	@Override
	public int getItemPosition(Object object) {
		// TODO Auto-generated method stub
		return super.getItemPosition(object);
	}

	@Override
	public Object instantiateItem(View container, int position) {
		// TODO Auto-generated method stub
		View view = views.get(position);
			((ViewPager) container).addView(view);
			return view;
	}

	@Override
	public Parcelable saveState() {
		// TODO Auto-generated method stub
		return null;
	}

	private static void log(String info) {
		Log.i("ViewPagerAdapter", info);
	}

}
